package com.csvreader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Test {
	
	public static void main(String[] args) {
		//hashCodeCompare();
		//String dateStr = DateTimeUtil.toString(date, DateTimeUtil.FORMAT_YYYYMMDD);
		String data_day="20170712";
		StringBuffer sb=new StringBuffer();
		DataSummary ds = new DataSummary("D:\\boochy\\data");
		List<MinuteSummary> ll=ds.minuteCalculation(data_day,DirectoryType.MINUTE);
		for (int i = 0; i < ll.size(); i++) {
			//System.out.println(ll.get(i));
			sb.append(ll.get(i)).append("\n");
		}
		
		List<MinuteSummary> ll5=ds.minuteCalculation(data_day,DirectoryType.MINUTE5);
		for (int i = 0; i < ll5.size(); i++) {
			//System.out.println(ll5.get(i));
			sb.append(ll5.get(i)).append("\n");
		}
		
		List<MinuteSummary> ll15=ds.minuteCalculation(data_day,DirectoryType.MINUTE15);
		for (int i = 0; i < ll15.size(); i++) {
			//System.out.println(ll15.get(i));
			sb.append(ll15.get(i)).append("\n");
		}
		System.out.println(sb.toString());
		write("D:\\boochy\\result\\"+data_day,sb.toString());
		
	}
	
	
	private static void write(String filename,String content) {
		try{
		      String data = content;

		      File file =new File(filename+".txt");

		      //if file doesnt exists, then create it
		      if(!file.exists()){
		    	  file.createNewFile();
		      }

		      //true = append file
		      FileWriter fileWritter = new FileWriter(file,true);
		             BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
		             bufferWritter.write(content);
		             bufferWritter.close();
		     }catch(IOException e){
		      e.printStackTrace();
		     }
	}
	
	/**
	 * 数组hashcode
	 */
	public static void hashCodeCompare(){
		List<Integer> list1 = new ArrayList<Integer>();
		List<Integer> list2 = new ArrayList<Integer>();
		System.out.println(list1.hashCode());
		System.out.println(list2.hashCode());
		
		list1.add(11111111);
		System.out.println(list1.hashCode());
		list2.add(11111111);
		System.out.println(list2.hashCode());
		
		list1.add(11111112);
		System.out.println(list1.hashCode());
		list2.add(11111112);
		System.out.println(list2.hashCode());
		
		list1.add(11111113);
		System.out.println(list1.hashCode());
		list2.add(11111114);
		System.out.println(list2.hashCode());
		
		list1.add(11111114);
		System.out.println(list1.hashCode());
		list2.add(11111113);
		System.out.println(list2.hashCode());
	}
	
	public static void read(){
		// heads min_time,open_px,close_px,high_px,low_px,business_amount,business_balance,is_open
        String filePath = "D:\\boochy\\data\\daily\\XSHE\\000856.csv";

        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(filePath);

            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()){
                // 读一整行
                System.out.print(csvReader.getRawRecord()+"-----");
                
                System.out.println(csvReader.getRawRecord().hashCode()+"-----");
                // 读这行的某一列
                System.out.println(csvReader.get("min_time"));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
	
	public static void write(String[] content){

        String filePath = "D:\\boochy\\data\\daily\\XSHE\\000856.csv";

        try {
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(filePath,',', Charset.forName("GBK"));
            //CsvWriter csvWriter = new CsvWriter(filePath);

            // 写表头
            //String[] content = {"12365","张山","34"};
            csvWriter.writeRecord(content);
            csvWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
